package com.erp.erp_ui.RoleLinkResource;

import com.erp.erp_entitys.RoleLinkResource.RoleLinkResourceEntity;
import com.erp.erp_servers.RoleLinkResource.IRoleLinkResourceService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleLinkResource
 * @Description 角色与资源控制类
 * @Author songjizhen
 * @Date 2020-07-11 21:28
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "RoleLinkResource")
 class RoleLinkResourceControl extends Base {
    @Autowired
    IRoleLinkResourceService roleLinkResourceService;
    /**
     * 新增
     * @param
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody RoleLinkResourceEntity rlr){//@RequestBody RoleLinkResourceEntity rlr
        try {
            Map<String,Boolean> map=this.getMap();
           /* RoleLinkResourceEntity rlr=new RoleLinkResourceEntity();
            rlr.setRoleCode(roleCode);
            rlr.setResourceCode(resourceCode);*/
            String[] objs=rlr.getResourceCode().split(",");//前端传来的菜单项目
            rlr.setResourceCode(null);
           roleLinkResourceService.deleteByExample(rlr);
            int num=0;
            for(int i=0;i<objs.length;i++){
                //不为空，
                if(!objs[i].equals("") ){//&& !(map.get(rlr.getRoleCode()+objs[i]))

                    rlr.setResourceCode(objs[i]);
                      num = roleLinkResourceService.insertSelective(rlr);
                }

            }

            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
     }
    private Map<String,Boolean> getMap(){
        Map<String,Boolean> map=new HashMap<>();
        RoleLinkResourceEntity rlre=new RoleLinkResourceEntity();
        List<RoleLinkResourceEntity> roleLinkResourceEntities = roleLinkResourceService.selectByExample(rlre);
        for(int i=0;i<roleLinkResourceEntities.size();i++){
            RoleLinkResourceEntity re=roleLinkResourceEntities.get(i);
            map.put(re.getRoleCode()+re.getResourceCode(),true);
        }
        return map;
    }
    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> delete(Integer id){
        try {
            RoleLinkResourceEntity rlre=new RoleLinkResourceEntity();
            rlre.setId(id);
            int num = roleLinkResourceService.deleteByExample(rlre);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 更新
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody RoleLinkResourceEntity rlre){
        try {
            int num = roleLinkResourceService.updateByExampleSelective(rlre);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取角色对应的资源
     * @param roleCode
     * @return
     */
    @RequestMapping(value = "selectByRoleCode",method = RequestMethod.POST)
    @ResponseBody
     public R<List<RoleLinkResourceEntity>> selectByRoleCode(String roleCode){
        try {
           RoleLinkResourceEntity rlre=new RoleLinkResourceEntity();
            rlre.setRoleCode(roleCode);
            List<RoleLinkResourceEntity> roleLinkResourceEntities = roleLinkResourceService.selectByExample(rlre);
           return R.build(roleLinkResourceEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 获取所 有资源关联列表
     * @return
     */
    @RequestMapping(value = "selectAll",method = RequestMethod.POST)
    @ResponseBody
     public R<List<RoleLinkResourceEntity>> selectAll(){
        try {
            RoleLinkResourceEntity rlre=new RoleLinkResourceEntity();
            List<RoleLinkResourceEntity> roleLinkResourceEntities = roleLinkResourceService.selectByExample(rlre);
            return R.build(roleLinkResourceEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
